package Test;

import vo.Student;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test5 {

   static Stream<String> names = Stream.of("John Lennon", "Paul McCartney",
            "George Harrison", "Ringo Starr", "Pete Best", "Stuart Sutcliffe");

    //computeIfAbsent
    public Student readStudentFromDB(String name){
       return new Student(2, "bb", "美国");
    }


    public void test1(String name){
        Map<String,Student> map=new HashMap<>();
        map.computeIfAbsent(name,this::readStudentFromDB);
    }

    //map迭代
    public static void test2(){
        Map<Student,Integer> map=new HashMap<>();

        Map<Student, List<String>> stMap=new HashMap<>();
        for (Map.Entry<Student, List<String>> entry : stMap.entrySet()) {
            List<String> value = entry.getValue();
            map.put(entry.getKey(),value.size());
        }

        stMap.forEach((k,v) ->{
            map.put(k,v.size());
        });
    }

    //练习题：找出长度最长的字符串
    public static void test3(){
        String s = names
                .collect(Collectors.maxBy(Comparator.comparing(e -> e.length())))
                .orElseThrow(() -> new RuntimeException());
        System.out.println(s);
    }


    //找出每个字符串在流中出现的次数
    public static void test4(){
        Stream<String> names = Stream.of("John", "Paul", "George", "John",
                "Paul", "John");
        Map<String, Long> collect = names.collect(Collectors.groupingBy(name -> name, Collectors.counting()));
        System.out.println(collect);
    }

    public static void main(String[] args) {
        Student student=new Student();
        test4();
    }



}
